package com.malty.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.malty.mall.dao.ums.UmsRoleResourceMapper;
import com.malty.mall.domain.entity.ums.UmsRoleResourceRelation;
import com.malty.mall.service.ums.IUmsRoleResourceService;
import com.malty.mall.util.PreconditionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author malty
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UmsRoleResourceServiceImpl extends ServiceImpl<UmsRoleResourceMapper, UmsRoleResourceRelation> implements IUmsRoleResourceService {
    @Override
    public Boolean createOrUpdateRelation(Integer roleId, Integer resourceId) {
        UmsRoleResourceRelation umsResource = new UmsRoleResourceRelation(roleId,resourceId);
        boolean update = this.saveOrUpdate(umsResource, Wrappers.<UmsRoleResourceRelation>lambdaQuery()
                .eq(UmsRoleResourceRelation::getRoleId, roleId)
                .eq(UmsRoleResourceRelation::getResourceId, resourceId));
        PreconditionUtils.check(!update, "创建失败");
        return true;
    }
}
